Clear["`*"];

b1 = Table[Random[], {30}, {10}];
b2 = Table[Random[], {10}, {30}];
b3 = Table[Random[], {30}, {10}];

b1 . b2 . b3; // Timing

Dot[b1, b2, b3]; // Timing

r = {30, 1, 40, 10, 25}

Clear[m];
\(\(m[i_, j_] /; i < j := \(m[i, j] = 
          Module[{choices, best}, 
			choices = 
              Table[m[i, k]\  + \ 
                  m[k + 1, 
                    j]
					\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  + r[\([i]\)] r[\([k + 1]\)] r[\([j + 1]\)], 
					{k, 
                  i, j - 1}]; 
			best = 
              Min[choices]; 
			path[i, 
                j] = \(Position[choices, best]\)[\([1, 1]\)] + i - 
                1; 
			best]\);\)\)
\(	\(m[__] := 0;\)\)

m[1, 4]

TableForm[Array[m, {4, 4}], TableHeadings -> Automatic]

path[__] := 0;
TableForm[Array[path, {4, 4}], TableHeadings -> Automatic]

?path

{1, 
          4} /. {i_Integer, j_Integer} /; 
            i < j -> {{i, path[i, j]}, {path[i, j] + 1, 
              j}}//Trace

{1, 
          4} /. {i_Integer, j_Integer} /; 
            i < j :> {{i, path[i, j]}, {path[i, j] + 1, 
              j}}//Trace

{1, 
        4} //. {i_Integer, j_Integer} /; 
          i < j :> {{i, path[i, j]}, {path[i, j] + 1, j}}

multiplicationorder = % /. {i_, i_} -> i
multiplicationorder /. List -> ""

\(showorder[i_, j_, p_] := 
      Module[{k, s1, s2}, 

        If[i == j, "A" <> ToString[i], 
	
          k = p[i, j]; {showorder[i, k, p], \ 
            showorder[k + 1, j, p]}]]\)

showorder[1, 4, path]

\(showorder1[i_, j_, p_] := 
      Module[{k, s1, s2}, 

        If[i == j, "A"<>ToString[i], 
	k = p[i, j]; 
          "("<>showorder1[i, k, p]<>"*"<>\ showorder1[k + 1, j, p]<>
            ")"]]\)

showorder1[1, 4, path]

\(computem[r_] := 
      Module[{n, mop, path, i, j, d, choices}, 
		n = Length[r] - 1; 
		
        Do[mop[i, j] = 0; path[i, j] = 0, {i, n}, {j, n}]; 
		
        For\ [d = 1, d <= n - 1, \(d++\), 
			
          For[i = 1, i <= n - d, \(i++\), 
				j = i + d; 
				
            choices = 
              Table[mop[i, k]\  + \ mop[k + 1, j]

                  						\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  + r[\([i]\)] r[\([k + 1]\)] r[\([j + 1]\)], 

                			\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 		{k, i, 
                  j - 1}]; 
			\ \ \ best = Min[choices]; 
            mop[i, j] = best; 
			\ \ \ 
            path[i, j] = \(Position[choices, best]\)[\([1, 1]\)] + i - 1]]; 

        			{mop, path}]\)

\({m, s} = computem[r]; 

    TableForm[Array[m, {4, 4}], TableHeadings -> Automatic]\)TableForm[Array[s, {4, 4}], TableHeadings -> Automatic]

showorder[1, 4, s]

showorder1[1, 4, s]